/*
dataabilityhelper模板
 */

import featureAbility from '@ohos.ability.featureAbility';
import dataAbility from '@ohos.data.dataAbility'
import {Book} from './bookModel';
import {User,todoItem} from './userModel';

const TAG = "dataAbilityHelperModel";

export namespace URI{
    /*
uri结构
scheme：协议方案名，固定为“dataability”，代表Data Ability所使用的协议类型
authority：设备ID。如果为跨设备场景，则为目标设备的ID；如果为本地设备场景，则不需要填写
path：资源的路径信息，代表特定资源的位置信息
query：查询参数
fragment：可以用于指示要访问的子资源
注:scheme与后面部分用"://"分隔,其后参数之间用"/"分隔
eg:"dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//user.admin"
eg:"dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility/tableName/library.4"
*/
    export const BASE_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility';
    export const User_admin_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//user.admin';
    export const User_reader_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//user.reader';
    export const User_adminTodo_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//user.adminTodo';
    export const Book_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//book';
    export const Library_root_URI = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility//library.1';
    export const Library_level4_book = 'dataability:///com.lx_wut.libraryManageSystem.DataManagementAbility/Level4book/library.4';
    /**
     * 生成URI,注意这里不需要完整tableName
     * @param tableName 注意这里不需要完整tableName
     * @param level in [2,3,4]
     */
    export function generateLibraryUri(tableName:string,level:number):string{
        switch(level){
//            case 1:{return BASE_URI + '/' + tableName + '/' + 'library.1';}
            case 2:{return BASE_URI + '/' + 'Level2' + tableName + '/' + 'library.2';}
            case 3:{return BASE_URI + '/' + 'Level3' + tableName + '/' + 'library.3';}
            case 4:{return BASE_URI + '/' + 'Level4' + tableName + '/' + 'library.4';}
            default:{console.info('[dataAbilityHelperModel] generateLibraryUri error');return null;}
        }
    }
}
export const DA_HELPER = featureAbility.acquireDataAbilityHelper(URI.BASE_URI)


export function bookRdbSearch(searchType:string,searchArea:string,searchKey:string):Book[]{

    return [
        new Book('0000000000000','TP314.86/65(1)','书名1',
            '子书名1','0000000000000','作者1','译者1','出版社1','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名2',
            '子书名2','0000000000000','作者2','译者2','出版社2','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名3',
            '子书名3','0000000000000','作者3','译者3','出版社3','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名4',
            '子书名4','0000000000000','作者4','译者4','出版社4','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名4',
            '子书名5','0000000000000','作者4','译者4','出版社4','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名4',
            '子书名6','0000000000000','作者4','译者4','出版社4','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名4',
            '子书名7','0000000000000','作者4','译者4','出版社4','2000',100),
        new Book('0000000000000','TP314.86/65(1)','书名4',
            '子书名8','0000000000000','作者4','译者4','出版社4','2000',100),
    ]
}

export namespace bookRdb{
}

export namespace libraryRdb{
}

namespace userRdb{

    export function insertTable_admin(userId:string,userPermission:number,UserName:string,password:string,token:string){
        let valuesBucket = {id:userId,permission:userPermission,name:UserName,password:password,token:token};
        DA_HELPER.insert(
            URI.User_admin_URI,
            valuesBucket,
            (err,data)=>{
            if(err.code){
                console.info(`${TAG} insertTable_admin err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} insertTable_admin successful`);
        }
        )
    }

    export function insertTable_reader(userId:string,userPermission:number,UserName:string,password:string,token:string,lend:string){
        let valuesBucket = {id:userId,permission:userPermission,name:UserName,password:password,token:token,lend:lend};
        DA_HELPER.insert(
            URI.User_reader_URI,
            valuesBucket,
            (err,data)=>{
            if(err.code){
                console.info(`${TAG} insertTable_reader err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} insertTable_reader successful`);
        }
        )
    }

    export function insertTable_adminTodo(todoType:string,todoId:string,todoText:string){
        let valuesBucket = {todoType:todoType,todoId:todoId,todoText:todoText};
        DA_HELPER.insert(
            URI.User_adminTodo_URI,
            valuesBucket,
            (err,data)=>{
            if(err.code){
                console.info(`${TAG} insertTable_adminTodo err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} insertTable_adminTodo successful`);
            }
        )
    }

    export function deleteTable_adminTodo(todoId:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('todoId',todoId);
        DA_HELPER.delete(
            URI.User_adminTodo_URI,
            predicates,
            (err, num) => {
            if(err.code){
                console.info(`${TAG} deleteTable_adminTodo err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} deleteTable_adminTodo successful`);
            console.info(TAG + ' delete num =' + num)
        })
    }

    export function deleteTable_admin(Id:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('id',Id);
        DA_HELPER.delete(
            URI.User_admin_URI,
            predicates,
            (err, num) => {
            if(err.code){
                console.info(`${TAG} deleteTable_admin err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} deleteTable_admin successful`);
            console.info(TAG + ' delete num =' + num)
        })
    }

    export function deleteTable_reader(Id:string){
        let predicates = new dataAbility.DataAbilityPredicates();
        predicates.equalTo('id',Id);
        DA_HELPER.delete(
            URI.User_reader_URI,
            predicates,
            (err, num) => {
            if(err.code){
                console.info(`${TAG} deleteTable_reader err` + JSON.stringify(err));
                return;
            }
            console.info(`${TAG} deleteTable_reader successful`);
            console.info(TAG + ' delete num =' + num)
        })
    }

    export function queryTable_admin(columns:string[]){
        //未完成
    }

    export function queryTable_adminTodo(callback:(res)=>void){
        let columns = ['todoType'];
        let predicates = new dataAbility.DataAbilityPredicates();
        DA_HELPER.query(
            URI.User_adminTodo_URI,
            columns,
            predicates,
            (err,ResultSet)=>{
                if(err.code){
                    console.info(`${TAG} queryTable_adminTodo err` + JSON.stringify(err));
                    return;
                }
                callback(ResultSet);
            }
        )
    }

    export function queryTable_reader(columns:string[]){
        //未完成
    }

    export function updateTable_admin(){
        //未完成
    }

    export function updateTable_adminTodo(){
        //未完成
    }

    export function updateTable_reader(){
        //未完成
    }
}